package com.graduation.mathematics.ui.activity;

import android.Manifest;
import android.content.Intent;
import android.os.SystemClock;

import com.graduation.mathematics.R;
import com.graduation.mathematics.base.BaseActivity;
import com.tbruyelle.rxpermissions2.Permission;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

import static com.blankj.utilcode.util.AppUtils.exitApp;

public class SplashActivity extends BaseActivity {

    private boolean afterGetPermission = false;
    private boolean afterInitApp = false;

    @Override
    protected int getLayout() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initDataAndEvent() {
        initPermissions();
    }

    /**
     * 申请权限
     */
    protected void initPermissions() {
        RxPermissions rxPermissions = new RxPermissions(this);
        // 定位权限是高版本获取Wifi名称需要的
        Disposable subscribe = rxPermissions.requestEachCombined(
                Manifest.permission.READ_EXTERNAL_STORAGE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.CAMERA)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Permission>() {
                    @Override
                    public void accept(Permission permission) {
                        if (permission.granted) { // 成功获取所有权限
                            afterGetPermission = true;
                            jumpToLogin();
                        } else if (permission.shouldShowRequestPermissionRationale) { // 选择禁止,但没有选择"以后不再询问"
                            showToast("获取权限失败,请添加权限");
                            Observable.timer(2, TimeUnit.SECONDS)
                                    .subscribe(new Consumer<Long>() {
                                        @Override
                                        public void accept(Long aLong) throws Exception {
                                            initPermissions();
                                        }
                                    });
                        } else { // 选择了"以后不在询问"
                            showToast("获取权限失败,请到\"设置\"中添加权限");
                            Observable.timer(2, TimeUnit.SECONDS)
                                    .subscribe(new Consumer<Long>() {
                                        @Override
                                        public void accept(Long aLong) throws Exception {
                                            exitApp();
                                        }
                                    });
                        }
                    }
                });
    }

    private void jumpToLogin() {
        new Thread() {
            @Override
            public void run() {
                SystemClock.sleep(3000);
                startActivity(new Intent(SplashActivity.this, LoginActivity.class));
                finish();
            }
        }.start();
    }
}
